package com.yami.shop.admin.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Gyl;
import com.yami.shop.bean.model.Phone;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.service.GylService;
import com.yami.shop.service.PhoneService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/prod/phone")
public class PhoneController {
    @Autowired
    private PhoneService phoneService;

    /**
     * 分页查询
     *
     * @param page    分页对象
     * @param
     * @return 分页数据
     */
    @GetMapping("/page")
    public ServerResponseEntity<IPage<Phone>> getPhonePage(PageParam<Phone> page, Phone phone) {
        IPage<Phone> phonePage = phoneService.page(
                page, new LambdaQueryWrapper<Phone>()
                        //.eq(prodTag.getStatus() != null, ProdTag::getStatus, prodTag.getStatus())
                        .like(phone.getMobile() != null, Phone::getMobile, phone.getMobile())
                        .like(phone.getUserName() !=null,Phone::getUserName,phone.getUserName())
        );

        return ServerResponseEntity.success(phonePage);

    }


    /**
     * 通过id查询手机号用户
     *
     * @param id id
     * @return 单个数据
     */
    @GetMapping("/info/{id}")
    public ServerResponseEntity<Phone> getById(@PathVariable("id") Long id) {
        return ServerResponseEntity.success(phoneService.getById(id));
    }

    /**
     * 新增手机号用户
     *
     * @  手机号用户
     * @return 是否新增成功
     */
    @SysLog("新增手机号用户")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('prod:phone:save')")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid Phone phone) {
        // 查看是否相同的标签
        List<Phone> list = phoneService.list(new LambdaQueryWrapper<Phone>()
                .eq(Phone::getMobile, phone.getMobile())
                .eq(Phone::getMallid,phone.getMallid())
        );
        if (CollectionUtil.isNotEmpty(list)) {
            throw new YamiShopBindException("商城手机号用户已存在，不能添加相同的手机号");
        }

        phone.setBalanceUsed(0);
        phone.setCreateTime(LocalDateTime.now());
        return ServerResponseEntity.success(phoneService.save(phone));
    }

    /**
     * 修改手机号用户
     *
     * @  手机号用户
     * @return 是否修改成功
     */
    @SysLog("修改手机号用户")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('prod:phone:update')")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid Phone phone) {

        Phone dbPhone=phoneService.getById(phone.getId());
        if(dbPhone.getBalanceUsed()==1){
            throw new YamiShopBindException("金额已经加到用户账户，不能修改");
        }
        phone.setBalanceUsed(dbPhone.getBalanceUsed());  //BalanceUsed字段不能改变

        return ServerResponseEntity.success(phoneService.updateById(phone));
    }

    /**
     * 通过id删除手机号用户
     *
     * @param id id
     * @return 是否删除成功
     */
    @SysLog("删除手机号用户")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('prod:phone:delete')")
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long id) {
        Phone phone = phoneService.getById(id);
        if(phone.getBalanceUsed()==1){
            throw new YamiShopBindException("金额已经加到用户账户，不能删除");
        }

        return ServerResponseEntity.success(phoneService.removeById(id));
    }

    @GetMapping("/listPhoneList")
    public ServerResponseEntity<List<Phone>> listPhoneList() {

        return ServerResponseEntity.success(phoneService.list());

    }
}
